<?php

class Reference extends AppModel {
	var $name= 'Reference';

//ACL this is a crontrolled object ACO
	var $actsAs = array('Acl' => array('type' => 'controlled'));
	
	function parentNode() {
		if (!$this->id && empty($this->data)) {
			return null;
		}
		if (isset($this->data['Reference']['application_id'])) {
			$appId = $this->data['Reference']['application_id'];
		} else {
			$appId = $this->field('application_id');
		}
		if (!$appId) {
			return null;
		} else {
			return array('Application' => array('id' => $appId));
		}
	}

function isEvaluated($id = null) {
	if(!$id) $id = $this->id;
	$evaluated = $this->findById($id, 'complete');
	return !!($evaluated['Reference']['complete']);
}

//
function isComplete($app_id) {
	
	return ($this->find('count',
							array('conditions' => array(
											'Reference.application_id' => $app_id,
											'Reference.complete' => true))) >= 3);
}

function checkAccess($id, $uid) {
	$ref = $this->read('uid', $id);
	//check that we have a correct ids, and uid.
	return (($id && $uid) && $uid == $ref['Reference']['uid']);
}

function beforeSave($options) {
	if(isset($this->data['Reference']['content'])) {
		$file = $this->data['Reference']['content'];
		$file_contents = file_get_contents($file['tmp_name']);
		if($file_contents) {
			$file_contents = base64_encode($file_contents);
			$this->set('content', $file_contents);
			$this->set('mime_type', $file['type']);
			$this->set('file_name', $file['name']);
			$this->set('file_size', $file['size']);
			$this->set('file_date', date('Y-m-d'));
			return true;
		} else {
			return false;
		}
	}
	return true;
}

function afterFind($results, $primary) {
	//if this was the primary find.
	if($primary) {
		//for each result, which contains a content field.
		//unencode the content field.
		foreach($results as $line => $result) {
			if(isset($result['Reference']['content'])) {
				$results[$line]['Reference']['content'] = base64_decode($result['Reference']['content']);
			}
		}
	}
	return $results;
}

	
/**
* Validation Criteria.
*	
*/
var $validate = array(
	  'id' => array(
		  'limitRefs' => array(
			  'rule' => array('limitCount', 5),
			  'message' => 'You are limited to 5 references.'
			)
		),
		'uid' => array(
		  'minlength' => array(
			  'rule' => array('minlength', 12),
			  'message' => 'Token Error.'
			)
		),
		'firstname' => array(
			'notempty' => array(
			  'rule' => array('notempty'),
			  'message' => 'Please enter a firstname.'
			)
		),
		'lastname' => array(
			'notempty' => array(
			 	 'rule' => array('notempty'),
			 	 'message' => 'Please enter a lastname.')
		),
		'phone' => array(
			'notempty' => array(
			  'rule' => array('notempty'),
			  'message' => 'Please enter a phone.'
			),
			'phonenum' => array(
				'rule' => '/^[-+().0-9 ]{9,}$/i',
				'message' => 'Please enter a valid phone number.'
			)
		),
		'email' => array(
			'notempty' => array(
			  'rule' => array('notempty'),
			  'message' => 'Please enter an email.'
			),
			'email' => array(
			  'rule' => array('email'),
			  'message' => 'Please enter a valid email address.'
			)
		),
		'content' => array(
			'upload' => array(
			  'rule' => array('isUploadedFile'),
			  'message' => 'Not an a valid file upload.'
			),
			'filetype' => array(
				'rule' => array(
					'isValidFileType', array(
						'application/pdf',                // pdf
          	'application/x-pdf', 
          	'application/acrobat', 
          	'text/pdf',
          	'text/x-pdf', 

          	'text/plain',                     // text
          	'application/rtf',
						'text/richtext',
						'text/rtf',
						'application/msword'
					)
				),
				'message' => 'Not a valid file type.'
			)
		)
	);

/**
* START ASSOCIATION INFORMATION
******************************************************************************
*/
 	
 	var $belongsTo = array(
 		'Application' => array(
 			'className' => 'Application',
			'foreignKey' => 'application_id'
		)
	); //end belongsTo array.
	

												
}
